package org.infodavid.common.model.impl;

import org.infodavid.common.model.IUser;
import org.infodavid.common.model.IUserGroup;
import org.infodavid.common.model.security.ISecurityIdentifier;

/**
 * The Class SecurityIdentifierImpl.
 */
public class SecurityIdentifierImpl extends AbstractBasicDataObjectImpl implements
    ISecurityIdentifier {

	/** serialVersionUID. */
	private static final long serialVersionUID = 1549072855151143126L;

	/** The user. */
	private IUser user;

	/** The group. */
	private IUserGroup group;

	/** The role. */
	private IUserGroup.EUserRole role;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected SecurityIdentifierImpl() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.ISecurityIdentifier#getUser()
	 */
	@Override
	public IUser getUser() {
		return user;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.ISecurityIdentifier#getGroup()
	 */
	@Override
	public IUserGroup getGroup() {
		return group;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.ISecurityIdentifier#getRole()
	 */
	@Override
	public IUserGroup.EUserRole getRole() {
		return role;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		user = null;
		group = null;
		role = null;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.security.ISecurityIdentifier#setUser(org.infodavid.common.model.s
	 * .IUser)
	 */
	public void setUser(final IUser value) {
		if (!equals(user, value)) {
			user = value;
			markModified(USER);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.security.ISecurityIdentifier#setGroup(org.infodavid.common.model.s
	 * .IUserGroup)
	 */
	public void setGroup(final IUserGroup value) {
		if (!equals(group, value)) {
			group = value;
			markModified(GROUP);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.security.ISecurityIdentifier#setRole(org.infodavid.common.model.s
	 * .IUserGroup.EUserRole)
	 */
	public void setRole(final IUserGroup.EUserRole value) {
		if (!equals(role, value)) {
			role = value;
			markModified(ROLE);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append(" [user=");
		builder.append(user);
		builder.append(" ,group=");
		builder.append(group);
		builder.append(" ,role=");
		builder.append(role);
		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getUser() == null ? 0 : getUser().hashCode());
		result = prime * result + (getGroup() == null ? 0 : getGroup().hashCode());
		result = prime * result + (getRole() == null ? 0 : getRole().hashCode());

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof ISecurityIdentifier)) {
			return false;
		}

		final ISecurityIdentifier other = (ISecurityIdentifier)obj;

		if (getUser() == null) {
			if (other.getUser() != null) {
				return false;
			}
		}
		else if (!getUser().equals(other.getUser())) {
			return false;
		}

		if (getGroup() == null) {
			if (other.getGroup() != null) {
				return false;
			}
		}
		else if (!getGroup().equals(other.getGroup())) {
			return false;
		}

		if (getRole() == null) {
			if (other.getRole() != null) {
				return false;
			}
		}
		else if (!getRole().equals(other.getRole())) {
			return false;
		}

		return true;
	}
}
